package com.app.service.impl.ajaxservice;

import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.util.ShapeUtilities;

import com.app.service.BaseService;
import com.app.service.IAjaxService;

@Deprecated
public class JreeChartCreateImpl extends BaseService implements IAjaxService {

	@Override
	public Map<?, ?> execute(HttpServletRequest request) {
	    JFreeChart localJFreeChart = createChart(createDataset());
	    String filename="";
		try {
			filename = ServletUtilities.saveChartAsPNG(localJFreeChart, 420, 300, null, request.getSession());
		} catch (IOException e) {
			e.printStackTrace();
		}  
		String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename;  
		Map<String,String> map = new HashMap<String,String>();
		map.put("url", graphURL);
		return map;
	}

	private static JFreeChart createChart(CategoryDataset paramCategoryDataset) {
		   JFreeChart localJFreeChart = ChartFactory.createLineChart("", "", "", paramCategoryDataset, PlotOrientation.VERTICAL, true, true, false);
		   TextTitle title = new TextTitle("点位图", new Font("宋体", Font.BOLD, 20));
		   localJFreeChart.setTitle(title); 
		   //解决图表底部中文乱码问题  
		   localJFreeChart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
		 
		    CategoryPlot localCategoryPlot = (CategoryPlot)localJFreeChart.getPlot();
		    // Y轴
		    NumberAxis localNumberAxis = (NumberAxis)localCategoryPlot.getRangeAxis();
		    localNumberAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		    
		  
		    LineAndShapeRenderer localLineAndShapeRenderer = (LineAndShapeRenderer)localCategoryPlot.getRenderer();
		    
		    // series 点（即数据点）可见  
		    localLineAndShapeRenderer.setBaseShapesVisible(true);  
	        // 显示数据点的数据  
		    localLineAndShapeRenderer  
	                .setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());   
	        // 显示折线图点上的数据  
		    localLineAndShapeRenderer.setBaseItemLabelsVisible(true); 
		    
		    localLineAndShapeRenderer.setSeriesShapesVisible(0, true);
		    localLineAndShapeRenderer.setSeriesShapesVisible(1, false);
		    localLineAndShapeRenderer.setSeriesShapesVisible(2, true);
		    localLineAndShapeRenderer.setSeriesLinesVisible(2, false);
		    localLineAndShapeRenderer.setSeriesShape(2, ShapeUtilities.createDiamond(4.0F));
		    localLineAndShapeRenderer.setDrawOutlines(true);
		    localLineAndShapeRenderer.setUseFillPaint(true);
		    localLineAndShapeRenderer.setBaseFillPaint(Color.white);
		    return localJFreeChart;
	}

	private static CategoryDataset createDataset() {
	    DefaultCategoryDataset localDefaultCategoryDataset = new DefaultCategoryDataset();
	   
	    localDefaultCategoryDataset.addValue(198.0D, "P01", "");

	    return localDefaultCategoryDataset;
	}

}